﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using ElingOA6_Admin_DSJCJ.Areas.Admin.Models;
using System.Text;
using ElingOA6_Admin_DSJCJ.Common;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    public class PlanSummaryReportQueryController :Common.AdminBaseController
    {
        //
        // GET: /Admin/PlanSummaryReportQuery/

        public ActionResult Index(string year)
        {
            if (year == null || year == "")
            {
                year = DateTime.Now.Year.ToString();
            }

            BLL.Document DocumentBLL = new BLL.Document();
            DataSet ds = DocumentBLL.GetYeat(Common.GlobalConstant.ENTITYNO);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ViewData["minyear"] = year;
                ViewData["maxyear"] = year;
            }
            else
            {

                ViewData["maxyear"] = DateTime.Now.Year;

                ViewData["minyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<int>("FYear")).Min();
            }

            ViewData["title"] = year + "年鄞州财政局各科室（中心、办）计划总结上报统计表";

            ViewData["table"] = GetTable(year);
            ViewData["year"] = year;
            ViewData["dqyear"] = year;
            return View();
        }

        public string GetTable(string year)
        {
            BLL.Document documentBLL = new BLL.Document();
            BLL.HRDept hRDeptBLL = new BLL.HRDept();
            List<Model.HRDept> HRDeptList = hRDeptBLL.GetListByFIsReportPlanSummary(Common.GlobalConstant.ENTITYNO);

            DataSet ds = documentBLL.GetPlanSummaryReport(year);
            StringBuilder table = new StringBuilder();

            int mouth = 12;

            if (year == DateTime.Now.Year.ToString())
            {
                if (DateTime.Now.Day > 25)
                {
                    mouth = DateTime.Now.Month;
                }
                else
                {
                    mouth = DateTime.Now.Month - 1;
                }
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
               
                List<PlanSummaryReport> planSummaryReportList = GetList(ds.Tables[0]);
                
                foreach (Model.HRDept model in HRDeptList)
                {
                    table.Append("<tr><td style='text-align:center' class='tdClass'>" + model.FDeptName + "</td>");
                    int sum = 0;
                    for (int i = 1; i < 13; i++)
                    {
                        if (i <= mouth)
                        {
                            List<PlanSummaryReport> select = (from nm in planSummaryReportList where nm.years == year && nm.months == i.ToString() && int.Parse(nm.dayss) <= 25 && nm.FDeptNo == model.FDeptNo select nm).ToList();
                            if (select.Count > 0)
                            {
                                table.Append("<td  style='text-align:center' class='tdClass'>是</td>");
                            }
                            else
                            {
                                table.Append("<td   style='text-align:center' class='tdClass'><font color='red'>否</font></td>");
                                sum++;
                            }
                        }
                        else
                        {
                            table.Append("<td  class='tdClass'></td>");
                        }
                    }
                    table.Append("<td  style='text-align:center'  class='tdClass'>" + sum + "</td>");
                    table.Append("</tr>");
                }
            }
            else
            {
                foreach (Model.HRDept model in HRDeptList)
                {
                    table.Append("<tr><td  style='text-align:center' class='tdClass'>" + model.FDeptName + "</td>");
                    int sum = 0;
                    for (int i = 1; i < 13; i++)
                    {
                        if (i <= mouth)
                        {
                            table.Append("<td  style='text-align:center' class='tdClass'><font color='red'>否</font></td>");
                            sum++;
                        }
                        else
                        {
                            table.Append("<td  style='text-align:center'  class='tdClass'></td>");
                        }
                    }
                    table.Append("<td  style='text-align:center'  class='tdClass'>" + sum + "</td>");
                    table.Append("</tr>");
                }
            }
            return table.ToString();
        }

        public System.Web.Mvc.FileResult GetExcel()
        {
            string year = Fetch.Post("hdyear");
            string fileName = "";
            if (Request.UserAgent != null)
            {
                string userAgent = Request.UserAgent.ToUpper();
                if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)
                {
                    fileName = ToUtf8String("鄞州财政局各科室（中心、办）计划总结上报统计表.xls");
                }
            }

            return File(CreateXLS(year), "application/vnd.ms-excel", fileName);
        }

        /// <summary>   
/// 解决下载名称在IE下中文乱码   
/// </summary>   
/// <param name="s"></param>   
/// <returns></returns>   
        private String ToUtf8String(String s)
        {
            StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (c >= 0 && c <= 255)
                {
                    sb.Append(c);
                }
                else
                {
                    byte[] b;
                    try
                    {
                        b = Encoding.UTF8.GetBytes(c.ToString());
                    }
                    catch (Exception ex)
                    {
                        b = new byte[0];
                    }
                    for (int j = 0; j < b.Length; j++)
                    {
                        int k = b[j];
                        if (k < 0) k += 256;

                        sb.Append("%" + Convert.ToString(k, 16).ToUpper());
                    }
                }
            }
            return sb.ToString();
        }



        private Byte[] CreateXLS(string year)
        {
            List<Model.XlsModel> xlsModels = new List<Model.XlsModel>();

            Model.XlsModel xlsModel = new Model.XlsModel();

            List<Model.XlsModel> xlsModels1 = createXlsTable(year);
            xlsModels.AddRange(xlsModels1);

            List<Model.XlsModel_Column> xlsModelColumns = new List<Model.XlsModel_Column>();
            Model.XlsModel_Column xmc = new Model.XlsModel_Column(); xmc.ColumnIndexEnd = 1; xmc.ColumnIndexStart = 0; xmc.Width = 20; xlsModelColumns.Add(xmc);
            return GenerateUtil.ExportToExcelReturnPathGetByte(".xls", year + "年鄞州财政局各科室（中心、办）计划总结上报统计表", xlsModels1.Count + 1, xlsModels, xlsModelColumns, new List<Model.XlsModel_Row>());
        }
        private List<Model.XlsModel> createXlsTable(string year)
        {
            BLL.Document documentBLL = new BLL.Document();
            BLL.HRDept hRDeptBLL = new BLL.HRDept();
            List<Model.HRDept> HRDeptList = hRDeptBLL.GetListByFIsReportPlanSummary(Common.GlobalConstant.ENTITYNO);

            DataSet ds = documentBLL.GetPlanSummaryReport(year);
            List<Model.XlsModel> xlsModels = new List<Model.XlsModel>();

            //增加项目
            int mouth = 12;

            if (year == DateTime.Now.Year.ToString())
            {
                if (DateTime.Now.Day > 25)
                {
                    mouth = DateTime.Now.Month;
                }
                else
                {
                    mouth = DateTime.Now.Month - 1;
                }
            }
            Model.XlsModel xm;

            #region  表头
            xm = new Model.XlsModel();
            xm.Title = "科室名称/月份";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);


            xm = new Model.XlsModel();
            xm.Title = "一月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "二月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);


            xm = new Model.XlsModel();
            xm.Title = "三月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "四月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);


            xm = new Model.XlsModel();
            xm.Title = "五月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "六月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);


            xm = new Model.XlsModel();
            xm.Title = "七月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "八月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);


            xm = new Model.XlsModel();
            xm.Title = "九月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "十月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);


            xm = new Model.XlsModel();
            xm.Title = "十一月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "十二月";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);

            xm = new Model.XlsModel();
            xm.Title = "本年未按时上报次数";
            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xm.RowIndex = 1;
            xlsModels.Add(xm);
            #endregion

            int i = 2;
            if (ds.Tables[0].Rows.Count > 0)
            {
                List<PlanSummaryReport> planSummaryReportList = GetList(ds.Tables[0]);
                foreach (Model.HRDept model in HRDeptList)
                {
                    xm = new Model.XlsModel();
                    xm.Title = model.FDeptName;
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = i;
                    xlsModels.Add(xm);
                    int sum = 0;
                    for (int j = 1; j < 13; j++)
                    {
                        if (j <= mouth)
                        {
                            List<PlanSummaryReport> select = (from nm in planSummaryReportList where nm.years == year && nm.months == j.ToString() && int.Parse(nm.dayss) <= 25 && nm.FDeptNo == model.FDeptNo select nm).ToList();
                            if (select.Count > 0)
                            {
                                xm = new Model.XlsModel();
                                xm.Title = "是";
                                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                                xm.RowIndex = i;
                                xlsModels.Add(xm);
                            }
                            else
                            {
                                xm = new Model.XlsModel();
                                xm.Title = "否";
                                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                                xm.RowIndex = i;
                                xlsModels.Add(xm);
                                sum++;
                            }
                        }
                        else
                        {
                            xm = new Model.XlsModel();
                            xm.Title = "";
                            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                            xm.RowIndex = i;
                            xlsModels.Add(xm);
                        }
                    }
                    xm = new Model.XlsModel();
                    xm.Title = sum.ToString();
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = i;
                    xlsModels.Add(xm);
                    i++;
                }
            }

            else
            {
                foreach (Model.HRDept model in HRDeptList)
                {
                    xm = new Model.XlsModel();
                    xm.Title = model.FDeptName;
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = i;
                    xlsModels.Add(xm);
                    int sum = 0;
                    for (int j = 1; j < 13; j++)
                    {
                        if (j <= mouth)
                        {
                            xm = new Model.XlsModel();
                            xm.Title = "否";
                            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                            xm.RowIndex = i;
                            xlsModels.Add(xm);
                            sum++;
                        }
                        else
                        {
                            xm = new Model.XlsModel();
                            xm.Title = "";
                            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                            xm.RowIndex = i;
                            xlsModels.Add(xm);
                        }
                    }
                    xm = new Model.XlsModel();
                    xm.Title = sum.ToString();
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = i;
                    xlsModels.Add(xm);

                    i++;
                }
            }

            return xlsModels;
        }



        public List<PlanSummaryReport> GetList(DataTable dt)
        {
            List<PlanSummaryReport> planSummaryReportList = new List<PlanSummaryReport>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PlanSummaryReport model = new PlanSummaryReport();
                model.counts = dt.Rows[i]["counts"].ToString();
                model.dayss = dt.Rows[i]["dayss"].ToString();
                model.FDeptNo = dt.Rows[i]["FDeptNo"].ToString();
                model.months = dt.Rows[i]["months"].ToString();
                model.years = dt.Rows[i]["years"].ToString();
                planSummaryReportList.Add(model);
            }
            return planSummaryReportList;
        }

    }
}
